import 'package:intl/intl.dart';
import 'package:flutter/material.dart';
import 'dart:async';

class DateTimeDemo extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => DateTimeState();
}

class DateTimeState extends State<DateTimeDemo> {
  DateTime dateTime = DateTime.now();

  TimeOfDay selectTime = TimeOfDay(hour: 9, minute: 40);

  _changeDateTime() async {
    final DateTime selectDateTime = await showDatePicker(
        context: context,
        initialDate: dateTime,
        firstDate: DateTime(1900),
        lastDate: DateTime(2100));

    if (selectDateTime == null) return;
    setState(() {
      dateTime = selectDateTime;
    });
  }

  _changeTimeofDay() async {
    final TimeOfDay timeOfDay =
        await showTimePicker(context: context, initialTime: selectTime);

    if (timeOfDay == null) return;
    setState(() {
      selectTime = timeOfDay;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('DateTimeDemo'),
        elevation: 0.0,
      ),
      body: Container(
        padding: EdgeInsets.all(16.0),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Row(
              children: <Widget>[
                InkWell(
                  onTap: _changeDateTime,
                  child: Row(
                    children: <Widget>[
                      Text(DateFormat.yMd().format(dateTime)),
                      Icon(Icons.arrow_drop_down)
                    ],
                  ),
                ),
                InkWell(
                  onTap: _changeTimeofDay,
                  child: Row(
                    children: <Widget>[
                      Text(selectTime.format(context)),
                      Icon(Icons.arrow_drop_down)
                    ],
                  ),
                ),
              ],
            )
          ],
        ),
      ),
    );
  }
}
